<?php

namespace app\controller;

use app\BaseController;
use app\model\Archives;
use app\model\Parameter;
use app\model\Track;
use app\model\User_character;

/**
 * 客户跟踪
 * Class Track
 * @package app\controller
 * @author 下雨吧จุ๊บ
 */
class TrackController extends BaseController
{
    /**
     * @apiGroup 客户跟踪
     * @apiVersion 1.0.0
     * @api {GET} /track  客户跟踪列表
     * @apiDescription 客户跟踪列表
     * @apiParam {String} listRows 每页数量(必填*)
     * @apiParam {String} page 当前页数(必填*)
     * @apiParam {String} archivesid 客户档案id(选填 1:填写,客户档案回访按钮;2:不填写:左侧导航客户跟踪接口)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 客户跟踪列表,
     * message: '获取客户跟踪列表成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function index()
    {
        $data = input();
        if (empty($data['listRows'])) {
            $data['listRows'] = '8';
        }
        if (empty($data['page'])) {
            $data['page'] = '1';
        }
        $map[] = ['t.title','like','%%'];
        if (!empty($data['map']['title'])){
            $map[] = ['t.title','like','%'. $data['map']['title'] .'%'];
        }
        if (empty($data['archivesid'])){
            //左侧菜单客户跟踪按钮
            $data['list'] = Track::alias('t')
                ->join(['user' => 'u'], 'u.id=t.userid')
                ->join(['parameter' => 'p'], 'p.id=t.trackstageid')
                ->join(['parameter' => 'p1'], 'p1.id=t.trackfunid')
                ->field('t.*,u.realname,p.title as trackstagetitle,p1.title as trackfuntitle')
                ->where($map)
                ->order('updatetime desc')
                ->paginate([
                    'list_rows' => $data['listRows'],
                    'page' => $data['page'],
                ]);
        }else{
            //客户档案中回访按钮
//            $data['list'] = Track::alias('t')
//                ->join(['user' => 'u'], 'u.id=t.userid')
//                ->join(['character' => 'c'], 'c.id=t.characterid')
//                ->join(['parameter' => 'p'], 'p.id=t.trackstageid')
//                ->join(['parameter' => 'p1'], 'p1.id=t.trackfunid')
//                ->where('archivesid',$data['archivesid'])
//                ->field('t.*,u.realname,c.title as charactertitle,p.title as trackstagetitle')
//                ->paginate([
//                    'list_rows' => $data['listRows'],
//                    'page' => $data['page'],
//                ]);
            $data['list'] = Track::where('archivesid',$data['archivesid'])->order('updatetime desc')->paginate([
                    'list_rows' => $data['listRows'],
                    'page' => $data['page'],
                ]);
        }
        success($data, '获取客户跟踪列表成功');
    }

    /**
     * @apiGroup 客户跟踪
     * @apiVersion 1.0.0
     * @api {GET} /track/create  新增
     * @apiDescription 新增界面
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 跟进阶段列表,
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function create()
    {
        //客户档案列表
        $data['archives'] = Archives::select();
        //获取跟进阶段列表
        $data['trackstage'] = Parameter::where('status', 1)->where('pid', 80)->select();
        //获取跟进方式列表
        $data['trackfun'] = Parameter::where('status', 1)->where('pid', 81)->select();
        //员工列表
        $data['user'] = \app\model\User::where('status',1)->select();
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 客户跟踪
     * @apiVersion 1.0.0
     * @api {POST} /track  新增保存
     * @apiDescription 新增保存
     * @apiParam {String} title 客户名称
     * @apiParam {String} archivesid 客户档案id
     * @apiParam {String} contacts 联系人
     * @apiParam {String} content 跟进内容
     * @apiParam {String} tracktime 跟进时间
     * @apiParam {String} trackfunid 跟进方式id
     * @apiParam {array} userid 跟进人id
     * @apiParam {String} trackstageid 跟进阶段id
     * @apiParam {String} status 状态
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '新增成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function save()
    {
        $data = input();
        //必填项验证
        if (empty($data['archivesid'])) {
            error('请选择客户');
        }
        //必填项验证
        if (empty($data['trackfunid'])) {
            error('请选择跟进方式');
        }
        //必填项验证
        if (empty($data['userid'])) {
            $data['userid'] = cache($_SESSION['token']['value'])['id'];
        }
        //必填项验证
        if (empty($data['trackstageid'])) {
            error('请选择跟进阶段');
        }
        //补充跟进部门
        $data['characterid'] = User_character::where('userid', $data['userid'])->find()['characterid'];
        //补充时间
        $data['createtime'] = time();
        $data['updatetime'] = time();
        //添加数据
        $result = Track::insert($data);
        if ($result) {
            success(array(), '新增成功');
        }
        error('新增失败');
    }

    /**
     * @apiGroup 客户跟踪
     * @apiVersion 1.0.0
     * @api {GET} /track/:id  编辑读取
     * @apiDescription 编辑页面(:id是指当前id,直接写在路由中)
     * @apiParam {String} id id
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: array
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function read()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        //客户档案列表
        $data['archives'] = Archives::select();
        //获取跟进阶段列表
        $data['trackstage'] = Parameter::where('status', 1)->where('pid', 80)->select();
        //获取跟进方式列表
        $data['trackfun'] = Parameter::where('status', 1)->where('pid', 81)->select();
        //员工列表
        $data['user'] = \app\model\User::where('status',1)->select();
        //返回会员详情
        $data['info'] = Track::where('id', $data['id'])->find();
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 客户跟踪
     * @apiVersion 1.0.0
     * @api {PUT} /track/:id  编辑保存
     * @apiDescription 编辑页面保存(:id是指当前id,直接写在路由中)
     * @apiParam {String} id 当前数据id
     * @apiParam {String} title 客户名称
     * @apiParam {String} contacts 联系人
     * @apiParam {String} content 跟进内容
     * @apiParam {String} tracktime 跟进时间
     * @apiParam {String} trackfunid 跟进方式id
     * @apiParam {String} characterid 跟进部门id
     * @apiParam {array} userid 跟进人id
     * @apiParam {String} trackstageid 跟进阶段id
     * @apiParam {String} status 状态
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '保存成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function update()
    {
        $data = input();
        //必填项验证
        if (empty($data['archivesid'])) {
            error('请选择客户');
        }
        //必填项验证
        if (empty($data['trackfunid'])) {
            error('请选择跟进方式');
        }
        //必填项验证
        if (empty($data['userid'])) {
            $data['userid'] = cache($_SESSION['token']['value'])['id'];
        }
        //必填项验证
        if (empty($data['trackstageid'])) {
            error('请选择跟进阶段');
        }
        //补充跟进部门
        $data['characterid'] = User_character::where('userid', $data['userid'])->find()['characterid'];
        //更新时间
        $data['updatetime'] = time();
        //更新数据
        $result = Track::where('id', $data['id'])->save($data);
        success(array(), '保存成功');
    }

    /**
     * @apiGroup 客户跟踪
     * @apiVersion 1.0.0
     * @api {DELETE} /track/0  删除
     * @apiDescription 删除(路由中直接写0,ids集合使用参数传递)
     * @apiParam {array} ids id集合
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '删除成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function delete()
    {
        $data = input();
        //必填项验证
        if (empty($data['ids'])) {
            error('520数据错误(Missing parameter id)');
        }
        $result = Track::whereIn('id', $data['ids'])->delete();
        success(array(), '删除成功');
    }
}
